package cn.edu.ctbu.lddweb.controller;

import cn.edu.ctbu.lddweb.pojo.Class;
import cn.edu.ctbu.lddweb.pojo.Course;
import cn.edu.ctbu.lddweb.pojo.Grade;
import cn.edu.ctbu.lddweb.pojo.Logon;
import cn.edu.ctbu.lddweb.service.ClassService;
import cn.edu.ctbu.lddweb.service.CourseService;
import cn.edu.ctbu.lddweb.service.GradeService;
import cn.edu.ctbu.lddweb.service.LogonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * 教职工管理Controller
 */
@Controller
public class TeacherController {
    @Autowired
    private LogonService logonService;

    @Autowired
    private CourseService courseService;

    @Autowired
    private GradeService gradeService;

    @Autowired
    private ClassService classService;

    //教职工管理页面
    @RequestMapping("/teacher/index")
    public String index(Model model, ModelMap map, Logon logon){
        List<Logon> logons = logonService.selectList(logon);
        model.addAttribute("logon",logons);

        //课程下拉
        Course course = new Course();
        List<Course> list = courseService.selectList(course);
        model.addAttribute("course",list);

        //班级下拉
        Class aclass = new Class();
        List<Class> list2 = classService.selectList(aclass);
        model.addAttribute("class",list2);

        //年级下拉
        Grade grade = new Grade();
        List<Grade> list1 = gradeService.selectList(grade);
        model.addAttribute("grade",list1);
        return "/teacher/index";
    }

    //搜索/重置
    @RequestMapping("/selectLogon")
    public String selectLogon(@RequestParam(value = "username",required = false) String username,@RequestParam(value = "className",required = false) String classname,@RequestParam(value = "gradeName",required = false) String gradeName,@RequestParam(value = "courseName",required = false) String courseName, Model model){
        Logon logon = new Logon();
        logon.setUsername(username);
        logon.setGradeName(gradeName);
        logon.setClassName(classname);
        logon.setCourseName(courseName);
        List<Logon> list = logonService.selectList(logon);
        model.addAttribute("logon",list);

        //课程下拉
        Course course = new Course();
        List<Course> list2 = courseService.selectList(course);
        model.addAttribute("course",list2);

        //班级下拉
        Class aclass = new Class();
        List<Class> list3 = classService.selectList(aclass);
        model.addAttribute("class",list3);

        //年级下拉
        Grade grade = new Grade();
        List<Grade> list1 = gradeService.selectList(grade);
        model.addAttribute("grade",list1);
        return "/teacher/index";
    }

    //新增页面
    @RequestMapping("/logon/insert")
    public String insert(Model model){
        //课程下拉
        Course course = new Course();
        List<Course> list2 = courseService.selectList(course);
        model.addAttribute("course",list2);

        //班级下拉
        Class aclass = new Class();
        List<Class> list3 = classService.selectList(aclass);
        model.addAttribute("class",list3);

        //年级下拉
        Grade grade = new Grade();
        List<Grade> list1 = gradeService.selectList(grade);
        model.addAttribute("grade",list1);
        return "/teacher/insert";
    }

    //新增操作
    @RequestMapping("/insertLogon")
    public String insertLogon(Logon logon){
        //需要判断年级名称在数据库中是否存在
        List<Logon> list = logonService.selectList(logon);
        if(list.size()!=0){
            return "/error111";
        }
        logonService.insertLogon(logon);
        return "/teacher/index";
    }


    //修改
    @RequestMapping("/logon/edit")
    public String edit(int id,Model model){
        Logon logon = logonService.findID(id);
        model.addAttribute("logon",logon);

        //课程下拉
        Course course = new Course();
        List<Course> list2 = courseService.selectList(course);
        model.addAttribute("course",list2);

        //班级下拉
        Class aclass = new Class();
        List<Class> list3 = classService.selectList(aclass);
        model.addAttribute("class",list3);

        //年级下拉
        Grade grade = new Grade();
        List<Grade> list1 = gradeService.selectList(grade);
        model.addAttribute("grade",list1);
        return "/teacher/edit";
    }

    //修改操作
    @RequestMapping("/updateLogon")
    public String updateLogon(Logon logon){
        Boolean boo = logonService.checkSupplierUnique(logon);
        if(boo){
            return "error111";
        }
        logonService.updateLogon(logon);
        return "/teacher/index";
    }

    //删除
    @RequestMapping("/deleteLogon")
    public String deleteLogon(int id){
        logonService.deleteLogon(String.valueOf(id));
        return "/teacher/index";
    }
}
